<?php
/**
 * 功能：
 * 说明：
 * 创建日期：
 * 更新日期：
 * 作者：Mirze
 * 补充说明： 
 * 
 */
class DB
{

	/**
	 * (功能描述) 获得一个数据库连接对象
	 *
	 * @param string $dsn_name 要使用的数据库连接
	 * @return
	 */
	static function getConn($dsn_name = null)
    {	
		if(empty($dsn_name)) {
			$dsn['host'] = 'localhost';
			$dsn['port'] = '3306';
			$dsn['login'] = 'root';
			$dsn['password'] = '123456';
			$dsn['driver']   = 'mysql';
			$dsn['database'] = 'thinkphp';
		} else {
			//获取DB配置$dsn
			$dsn = array();
		    
		}

		$db_type = trim($dsn['driver']);
		$class_name = ucfirst($db_type);

		$driver_path = dirname(__FILE__) . "/adapter/" . strtolower($db_type) . ".class.php";
		include_once($driver_path);

        $dbo = new $class_name($dsn);

		return $dbo;
	}

	/**
	 * (功能描述) 将字符串形式的 DSN 转换为数组
     *
     * @code php
     * $string = 'mysql://root:mypass@localhost/test';
	 * $dsn = DB::parseDSN($string);
	 * @end code
	 *
	 * @param string  要分析的 DSN 字符串
	 * @return array
	 */
	static function parseDSN($dsn)
    {
        $dsn = str_replace('@/', '@localhost/', $dsn);
        $parse = parse_url($dsn);
        if (empty($parse['scheme'])) {
            return false;
        }

        $dsn = array();
        $dsn['host']     = isset($parse['host']) ? $parse['host'] : 'localhost';
        $dsn['port']     = isset($parse['port']) ? $parse['port'] : '';
        $dsn['login']    = isset($parse['user']) ? $parse['user'] : '';
        $dsn['password'] = isset($parse['pass']) ? $parse['pass'] : '';
        $dsn['driver']   = isset($parse['scheme']) ? strtolower($parse['scheme']) : '';
        $dsn['database'] = isset($parse['path']) ? substr($parse['path'], 1) : '';

        return $dsn;
    }
}
?>